/*------------ Create By Suriyakamol ---------*/
var menutimedelay = 0;
var menudisplay = "";
var old_style=null;
var old_obj=null;
var el_old=null;
var id_old=null;
var old_mode=null;
var old_obj_rp=null;
var old_style_rp=null;
//var new_style=null;

function Inint_AJAX() {
	try { return new ActiveXObject("Msxml2.XMLHTTP");    } catch(e) {}
	try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {}
	try { return new XMLHttpRequest(); } catch(e) {}
	alert("XMLHttpRequest not supported");
	return null;
}

function onclk_rp(obj_rp){
	if(old_obj_rp == obj_rp) return 0;
	obj_rp.className='onclk_report';
	if(old_obj_rp != null){
		old_obj_rp.className='noclk_report';
	}
	
	old_obj_rp=obj_rp;
	//old_bg_rp=obj.className;

	if(document.getElementById("case_onclk").value == "member"){
		if(document.getElementById("td_member") != obj_rp){
			document.getElementById("td_member").className='noclk_report';
			//document.getElementById("td_member").onmouseout=function (){ document.getElementById("td_member").style.background='#FAF0E6'; }
			//document.getElementById("td_member").onmouseover=function (){ document.getElementById("td_member").style.background='#FFCCFF'; }
			document.getElementById("case_onclk").value='';
		}
	}
}

function onclk_menu(obj){
	if(old_obj== obj) return 0;
	obj.className="onclk_sub_left_menu";
	if(old_obj != null){
		old_obj.className="sub_left_menu";
	}
	old_obj=obj;
	old_style=obj.className;
}

function clear_content(){
	var menutimedelay = 0;
	var menudisplay = "";
	var el_old=null;
	var id_old=null;
	var old_mode=null;
	var old_obj_rp=null;
	var old_style_rp=null;
}


function load_main(){
	var height_table=(window.innerHeight) ? window.innerHeight : document.body.clientHeight;
	var width_table=(window.innerWidth) ? window.innerWidth : document.body.clientWidth;
	

	var array_element=Array();
	var id_element=null;
	var h_div=15;
	var h_div_scroll;
	array_element[0]='product';
	array_element[1]='sub_product';
	array_element[2]='group_product';
	array_element[3]='customer';
	array_element[4]='staff';
	array_element[5]='department';
	array_element[6]='order_stock';
	array_element[7]='journal';
	array_element[8]='account';
	array_element[9]='st_ord_purchase';
	array_element[10]='comment_prd';

	for(n=0;n<array_element.length;n++){
		id_element=array_element[n];
		if(id_element == "staff") h_div=20;
		if(id_element == "product"){
			h_div_scroll=height_table-150;
		}else if(id_element == "st_ord_purchase" || id_element == "customer" || id_element == "comment_prd"){
			h_div_scroll=height_table-120;
		}else if(id_element == "department"){
			h_div_scroll=height_table-220;
		}else{
			h_div_scroll=(height_table/2)-h_div;
		}

		if(document.getElementById("div_data_"+id_element)){
			document.getElementById("div_data_"+id_element).style.height=h_div_scroll+"px";
		}
	}
}

//-------------- Hidden Menu ---------------

function hide_menu(id_element){
	document.getElementById("sub_tr_"+id_element).style.display="none";
	document.getElementById("mn_tr_"+id_element).onclick=function(){ show_menu(id_element); }
}

//------------- Show Menu ---------------
function show_menu(id_element){
	document.getElementById("sub_tr_"+id_element).style.display="";
	document.getElementById("mn_tr_"+id_element).onclick=function(){ hide_menu(id_element); }
}



function goto_page(dir_element){
	clear_content();
	var req=Inint_AJAX();
	var url=null;
	var value_url=null;
	
	if(dir_element == "mainreport") {
		window.location='mainreport.php';
		return 0;
	}
	url=dir_element+'.php';
	value_url='hash='+new Date().getTime()+Math.random();
	fn_delete_img(); 

	req.open("POST", url, true);
			req.onreadystatechange = function() {
					if (req.readyState==4) {
						if (req.status==200) {
							var data=eval(req.responseText);
							document.getElementById("show_data").innerHTML=data[0].result_data;
							/*if(dir_element != "product"){
								fn_delete_img();
							}*/
							load_main();
						}
					}
			};
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;"); // setHeader charset=tis-620
	req.send(value_url);
}

function clickupload(){
	var file_uld=document.getElementById("fileupload").value;
	var type_chk=null;
	var type_file=null;
	if(document.getElementById("fileupload").value.length == 0){
		alert('ระบุ File ที่จะ Upload'); return false ;
	}
	type_chk=file_uld.split('.');
	type_file=type_chk[type_chk.length-1];
	
	if(type_file == 'jpg' || type_file =='png'){
		document.getElementById('upmsg').innerHTML = 'กำลัง Upload อยู่ รอซักครู่...' ;
		document.getElementById('btnUpload').value = "ส่งไปแล้วไม่ต้องคลิกซ้ำ";
		document.getElementById('btnUpload').disabled = true ;
		return true ;	
	}
	alert('กรุณา Upload File ที่มีนามสกุล jpg, png เท่านั้น');
	return false;
	
}

function del_onclick(var_len){
	var i, var_file;
	for(i=0;i<var_len;i++){
		var_file=document.getElementById("file_product_"+i).value;
		document.getElementById("img_del_"+i).onclick=function(){ del_select(var_file); }
	}
	return true;
}

function uploadok(pathfile){
	var file_chk=null;
	file_chk=pathfile.split('|');
	document.getElementById('upmsg').innerHTML=file_chk[0];
	var len_id=parseInt(file_chk[file_chk.length-1]);
	document.getElementById('photo_history').value=file_chk[file_chk.length-2];
	
	document.getElementById('fileupload').value ="";
	document.getElementById('btnUpload').value = "Attachment";
	document.getElementById('btnUpload').disabled = false;
	if(len_id > 0){
		setTimeout("del_onclick('"+len_id+"')", 100);
	}
	//document.getElementById('frmUpload').reset();
	return true ;
}

/*function fn_reset(var_element){
	switch(var_element){
		case "product" : {
			document.getElementById("ownership").value='';
			document.getElementById("product_name_th").value='';
			document.getElementById("product_name_en").value='';
			document.getElementById("property").value='';
			document.getElementById("ingredients").value='';
			document.getElementById("net_weight").value='';
			document.getElementById("product_price").value='';
			document.getElementById("instruction").value='';
			document.getElementById("remark").value='';
			document.getElementById("upmsg").value='';
			document.getElementById('frmUpload').reset();
		}break;
	}
}*/

function show_tr(tr_n_element){
	document.getElementById(tr_n_element).style.display="";
	document.getElementById("sh_"+tr_n_element).onclick=function(){ hide_tr(tr_n_element); }
	load_main();
}

function hide_tr(tr_n_element){
	document.getElementById(tr_n_element).style.display="none";
	document.getElementById("sh_"+tr_n_element).onclick=function(){ show_tr(tr_n_element); }
	load_main();
}

/*function fn_update(url_element){
	var req=Inint_AJAX();
	var url=null;
	var value_url=null;
	
	url='add_'+url_element+'.php';

}*/
function trim_word(word_value){
	//if(word_value == null) return word_new;
	var word_new=null, n=0;
	for(i=0;i<word_value.length;i++){
		if(word_value[i] == " ") continue;
		if(n == 0) word_new=word_value[i];
		else word_new+=word_value[i];
		n++;
	}
	return word_new;
}

function fn_mode(obj_curr, st_mode, mode, url_element, id_element){
	var req=Inint_AJAX();
	var url=null;
	var value_url=null;
	if(document.getElementById("link_product")){
		var obj_prd=document.getElementById("link_product");
	}
	url='mod_'+url_element+'.php';

	value_url='mode='+mode;
	value_url+='&id='+id_element;
	value_url+='&hash='+new Date().getTime()+Math.random();

	req.open("POST", url, true);
			req.onreadystatechange = function() {
					if (req.readyState==4) {
						if (req.status==200) {
							var data=eval(req.responseText);
							var st_onclk;
							document.getElementById("menu_"+url_element).innerHTML = data[0].result_data;
							if(st_mode == "visible"){
								st_onclk="hidden";
							}else{
								st_onclk="visible";
							}
							if(mode == "new"){
								obj_curr.onclick=function(){ fn_mode(obj_curr, st_onclk, 'new', 'product', 'null'); }
								if(el_old != null && el_old != obj_curr){
									el_old.onclick=function(){ fn_mode(el_old, 'visible', 'edit', 'product', id_old); }
								}
								el_old=obj_curr;
								old_mode=mode;
							}else if(mode == "edit"){
								obj_curr.onclick=function(){ fn_mode(obj_curr, st_onclk, 'edit', 'product', id_element); }
								obj_prd.onclick=function(){ fn_mode(obj_prd, 'visible', 'new', 'product', 'null'); }
								el_old=obj_curr;
								id_old=id_element;
								old_mode=mode;
							}
							document.getElementById("menu_"+url_element).style.visibility=st_mode;
							//curobj.onclick=function(){ menu_hidden("hidden", curobj, "menu_"+url_element); }
						}
					}
			};
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;"); // setHeader charset=tis-620
	req.send(value_url);
}

function fn_add(url_element){
	var req=Inint_AJAX();
	var url=null;
	var value_url=null;
	
	url='mod_'+url_element+'.php';
	
	value_url='mode=add';
	document.getElementById("btn_add").disabled = true;
	document.getElementById("btn_cancel").disabled = true;
switch(url_element){
	case "product" :{

		value_url+='&product_id='+encodeURIComponent(document.getElementById("product_id").value);
		value_url+='&ownership='+encodeURIComponent(document.getElementById("ownership").value);
		value_url+='&product_name_th='+encodeURIComponent(document.getElementById("product_name_th").value);
		value_url+='&product_name_en='+encodeURIComponent(document.getElementById("product_name_en").value);
		value_url+='&property='+encodeURIComponent(document.getElementById("property").value);
		value_url+='&ingredients='+encodeURIComponent(document.getElementById("ingredients").value);
		value_url+='&net_weight='+encodeURIComponent(document.getElementById("net_weight").value);
		value_url+='&product_price='+encodeURIComponent(document.getElementById("product_price").value);
		value_url+='&instruction='+encodeURIComponent(document.getElementById("instruction").value);
		value_url+='&remark='+encodeURIComponent(document.getElementById("remark").value);
		value_url+='&status_import='+parseInt(document.getElementById("status_import").value);
		var obj_sub_group_id=document.getElementById("sub_group_id");
		var sub_group_id=obj_sub_group_id.options[obj_sub_group_id.selectedIndex].value;
		value_url+='&sub_group_id='+sub_group_id;
		value_url+='&photo_history='+encodeURIComponent(document.getElementById("photo_history").value);
		value_url+='&hash='+new Date().getTime()+Math.random();
	}break;

	case "sub_product" :{
		value_url+='&sub_product_id='+encodeURIComponent(document.getElementById("sub_product_id").value);
		value_url+='&sub_product_desc='+encodeURIComponent(document.getElementById("sub_product_desc").value);
		var obj_group_product_id=document.getElementById("group_product_id");
		var group_product_id=obj_group_product_id.options[obj_group_product_id.selectedIndex].value;
		value_url+='&group_product_id='+encodeURIComponent(group_product_id);
		value_url+='&hash='+new Date().getTime()+Math.random();

	}break;

	case "group_product" :{
		value_url+='&group_product_id='+encodeURIComponent(document.getElementById("group_product_id").value);
		value_url+='&group_product_desc='+encodeURIComponent(document.getElementById("group_product_desc").value);
		value_url+='&hash='+new Date().getTime()+Math.random();
	}break;

	case "staff" :{
		var passwd=trim_word(document.getElementById("passwd").value);
		var re_passwd=trim_word(document.getElementById("re_passwd").value);
		if(passwd == null || re_passwd == null) return alert('กรุณากรอก password หรือ Re-password !!!');
		value_url+='&username='+encodeURIComponent(document.getElementById("username").value);
		value_url+='&passwd='+encodeURIComponent(hex_md5(passwd));
		value_url+='&re_passwd='+encodeURIComponent(hex_md5(re_passwd));
		
		var obj_level_staff=document.getElementById("level_staff");
		var level_staff=obj_level_staff.options[obj_level_staff.selectedIndex].value;
		value_url+='&level_staff='+encodeURIComponent(level_staff);
		value_url+='&first_name='+encodeURIComponent(document.getElementById("first_name").value);
		value_url+='&last_name='+encodeURIComponent(document.getElementById("last_name").value);
		var obj_department_id=document.getElementById("department_id");
		var department_id=obj_department_id.options[obj_department_id.selectedIndex].value;
		value_url+='&department_id='+encodeURIComponent(department_id);
		value_url+='&hash='+new Date().getTime()+Math.random();
	}break;

	case "department" :{
		value_url+='&department_name='+encodeURIComponent(document.getElementById("department_name").value);
		value_url+='&hash='+new Date().getTime()+Math.random();
	}break;

	case "order_stock" :{
		var obj_product_id=document.getElementById("product_id");
		var product_id=obj_product_id.options[obj_product_id.selectedIndex].value;

		value_url+='&product_id='+encodeURIComponent(product_id);
		value_url+='&amount='+encodeURIComponent(document.getElementById("amount").value);
		value_url+='&price='+encodeURIComponent(document.getElementById("price").value);
		value_url+='&receive_date='+encodeURIComponent(document.getElementById("receive_date").value);
		value_url+='&hash='+new Date().getTime()+Math.random();
	}break;

	case "journal" :{
		var obj_product_id=document.getElementById("related_prd");
		var product_id=obj_product_id.options[obj_product_id.selectedIndex].value;

		value_url+='&journal_title='+encodeURIComponent(document.getElementById("journal_title").value);
		value_url+='&journal_desc='+encodeURIComponent(document.getElementById("journal_desc").value);
		value_url+='&product_id='+encodeURIComponent(product_id);
		value_url+='&hash='+new Date().getTime()+Math.random();
	}break;

	case "account" :{
		value_url+='&bank_name='+encodeURIComponent(document.getElementById("bank_name").value);
		value_url+='&bank_branch='+encodeURIComponent(document.getElementById("bank_branch").value);
		value_url+='&account_name='+encodeURIComponent(document.getElementById("account_name").value);
		value_url+='&account_number='+encodeURIComponent(document.getElementById("account_number").value);
		value_url+='&account_type='+encodeURIComponent(document.getElementById("account_type").value);
		value_url+='&hash='+new Date().getTime()+Math.random();
	}break;
}

	req.open("POST", url, true);
			req.onreadystatechange = function() {
					if (req.readyState==4) {
						if (req.status==200) {
							var data=eval(req.responseText);
							document.getElementById("btn_add").disabled = false;
							document.getElementById("btn_cancel").disabled = false;
								if(data[0].success == "false"){
									alert(data[0].comment);
									return 0;
								}
								
								goto_page(url_element);
						}
					}
			};
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;"); // setHeader charset=tis-620
	req.send(value_url);
}

function fn_edit(url_element, id_element){
	var req=Inint_AJAX();
	var url=null;
	var value_url=null;
	fn_delete_img();

	//if(url_element == "group_product") return alert('Edit ID='+id_element);
	url='mod_menu.php';
	value_url='case_select='+url_element+'&mode=edit&id='+id_element;
	value_url+='&hash='+new Date().getTime()+Math.random();
	
	req.open("POST", url, true);
			req.onreadystatechange = function() {
					if (req.readyState==4) {
						if (req.status==200) {
							var data=eval(req.responseText);
							document.getElementById("menu_"+url_element).innerHTML=data[0].result_data;
							if(url_element == "product"){
								document.getElementById("menu_"+url_element).style.visibility = "visible";
							}
						}
					}
			};
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;"); // setHeader charset=tis-620
	req.send(value_url);
}

/*function fn_edit(st_obj, url_element, id_element){
	var req=Inint_AJAX();
	var url=null;
	var value_url=null;
	fn_delete_img();
	
	//if(url_element == "group_product") return alert('Edit ID='+id_element);
	url='mod_menu.php';
	value_url='case_select='+url_element+'&mode=edit&id='+id_element;
	value_url+='&hash='+new Date().getTime()+Math.random();
	
	req.open("POST", url, true);
			req.onreadystatechange = function() {
					if (req.readyState==4) {
						if (req.status==200) {
							var data=eval(req.responseText);
							document.getElementById("menu_"+url_element).innerHTML=data[0].result_data;
						}
					}
			};
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;"); // setHeader charset=tis-620
	req.send(value_url);
}*/

function fn_update(url_element){
	var req=Inint_AJAX();
	var url=null;
	var value_url=null;
	
	url='mod_'+url_element+'.php';

	value_url='mode=update';

	document.getElementById("btn_save").disabled = true;
	document.getElementById("btn_cancel").disabled = true;
switch(url_element){
	case "product" :{

		value_url+='&product_id='+encodeURIComponent(document.getElementById("product_id").value);
		value_url+='&ownership='+encodeURIComponent(document.getElementById("ownership").value);
		value_url+='&product_name_th='+encodeURIComponent(document.getElementById("product_name_th").value);
		value_url+='&product_name_en='+encodeURIComponent(document.getElementById("product_name_en").value);
		value_url+='&property='+encodeURIComponent(document.getElementById("property").value);
		value_url+='&ingredients='+encodeURIComponent(document.getElementById("ingredients").value);
		value_url+='&net_weight='+encodeURIComponent(document.getElementById("net_weight").value);
		value_url+='&product_price='+encodeURIComponent(document.getElementById("product_price").value);
		value_url+='&instruction='+encodeURIComponent(document.getElementById("instruction").value);
		value_url+='&remark='+encodeURIComponent(document.getElementById("remark").value);
		var obj_status_import=document.getElementById("status_import");
		var status_import=obj_status_import.options[obj_status_import.selectedIndex].value;
		value_url+='&status_import='+parseInt(status_import);
		var obj_sub_group_id=document.getElementById("sub_group_id");
		var sub_group_id=obj_sub_group_id.options[obj_sub_group_id.selectedIndex].value;
		value_url+='&sub_group_id='+sub_group_id;
		value_url+='&photo_history='+encodeURIComponent(document.getElementById("photo_history").value);
		value_url+='&hash='+new Date().getTime()+Math.random();
	}break;

	case "sub_product" :{
		value_url+='&sub_product_id='+encodeURIComponent(document.getElementById("sub_product_id").value);
		value_url+='&sub_product_desc='+encodeURIComponent(document.getElementById("sub_product_desc").value);
		var obj_group_product_id=document.getElementById("group_product_id");
		var group_product_id=obj_group_product_id.options[obj_group_product_id.selectedIndex].value;
		value_url+='&group_product_id='+encodeURIComponent(group_product_id);
		value_url+='&hash='+new Date().getTime()+Math.random();
	}break;

	case "group_product" :{
		value_url+='&group_product_id='+encodeURIComponent(document.getElementById("group_product_id").value);
		value_url+='&group_product_desc='+encodeURIComponent(document.getElementById("group_product_desc").value);
		value_url+='&hash='+new Date().getTime()+Math.random();
	}break;

	case "staff" :{
		var new_passwd=trim_word(document.getElementById("new_passwd").value);
		var renew_passwd=trim_word(document.getElementById("renew_passwd").value);
		//if(new_passwd == null || renew_passwd == null) return alert('กรุณากรอก password หรือ Re-password !!!');
		value_url='user_id='+encodeURIComponent(document.getElementById("user_id").value);
		value_url+='&username='+encodeURIComponent(document.getElementById("username").value);
		if(new_passwd != null){
			value_url+='&new_passwd='+encodeURIComponent(hex_md5(new_passwd));
		}
		if(renew_passwd != null){
			value_url+='&renew_passwd='+encodeURIComponent(hex_md5(renew_passwd));
		}

		/*var obj_level_staff=document.getElementById("level_staff");
		var level_staff=obj_level_staff.options[obj_level_staff.selectedIndex].value;*/
		//return alert(level_staff);
		value_url+='&level_staff='+encodeURIComponent(document.getElementById("level_staff").value);
		value_url+='&first_name='+encodeURIComponent(document.getElementById("first_name").value);
		value_url+='&last_name='+encodeURIComponent(document.getElementById("last_name").value);
		var obj_department_id=document.getElementById("department_id");
		var department_id=obj_department_id.options[obj_department_id.selectedIndex].value;
		value_url+='&department_id='+encodeURIComponent(department_id);
		value_url+='&hash='+new Date().getTime()+Math.random();
	}break;

	case "journal" :{
		var obj_product_id=document.getElementById("related_prd");
		var product_id=obj_product_id.options[obj_product_id.selectedIndex].value;
		
		value_url+='&journal_id='+encodeURIComponent(document.getElementById("journal_id").value);
		value_url+='&journal_title='+encodeURIComponent(document.getElementById("journal_title").value);
		value_url+='&journal_desc='+encodeURIComponent(document.getElementById("journal_desc").value);
		value_url+='&product_id='+encodeURIComponent(product_id);
		value_url+='&hash='+new Date().getTime()+Math.random();
	}break;

	case "department" :{
		value_url+='&department_id='+encodeURIComponent(document.getElementById("department_id").value);
		value_url+='&department_name='+encodeURIComponent(document.getElementById("department_name").value);
		value_url+='&hash='+new Date().getTime()+Math.random();
	}break;

	case "order_stock" :{
		value_url+='&product_id='+encodeURIComponent(document.getElementById("product_id").value);
		value_url+='&check_id='+encodeURIComponent(document.getElementById("check_id").value);
		value_url+='&old_amount='+encodeURIComponent(document.getElementById("old_amount").value);
		value_url+='&new_amount='+encodeURIComponent(document.getElementById("new_amount").value);
		value_url+='&price='+encodeURIComponent(document.getElementById("price").value);
		value_url+='&receive_date='+encodeURIComponent(document.getElementById("receive_date").value);
		value_url+='&hash='+new Date().getTime()+Math.random();
	}break;

	case "account" :{
		value_url+='&account_id='+encodeURIComponent(document.getElementById("account_id").value);
		value_url+='&bank_name='+encodeURIComponent(document.getElementById("bank_name").value);
		value_url+='&bank_branch='+encodeURIComponent(document.getElementById("bank_branch").value);
		value_url+='&account_name='+encodeURIComponent(document.getElementById("account_name").value);
		value_url+='&account_number='+encodeURIComponent(document.getElementById("account_number").value);
		value_url+='&account_type='+encodeURIComponent(document.getElementById("account_type").value);
		value_url+='&hash='+new Date().getTime()+Math.random();
	}break;
}

	req.open("POST", url, true);
			req.onreadystatechange = function() {
					if (req.readyState==4) {
						if (req.status==200) {
							var data=eval(req.responseText);
								document.getElementById("btn_save").disabled = false;
								document.getElementById("btn_cancel").disabled = false;
								alert(data[0].comment);
								if(data[0].success == "false"){
									return 0;
								}
								goto_page(url_element);
						}
					}
			};
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;"); // setHeader charset=tis-620
	req.send(value_url);
}

function fn_cancel(url_element){
	goto_page(url_element);
}

function del_select(element_var){
	var req=Inint_AJAX();
	var url=null;
	var value_url=null;
	url='unlink_image.php';
	value_url='file_name='+encodeURIComponent(element_var);

	if(document.getElementById("photo_history")){
		if(document.getElementById("photo_history").value != ""){
			var path_image=encodeURIComponent(document.getElementById("photo_history").value);
			value_url+='&path_image='+path_image;
		}
	}
	if(document.getElementById("product_id")){
		value_url+='&product_no='+encodeURIComponent(document.getElementById("product_id").value);
	}

	value_url+='&hash='+new Date().getTime()+Math.random();
	
			req.open("POST", url, true);
				req.onreadystatechange = function() {
					if (req.readyState==4) {
						if (req.status==200) {
							var data=eval(req.responseText);
							if(data[0].case_del == "unlink"){
								document.getElementById("photo_history").value=data[0].photo_history;
								document.getElementById('upmsg').innerHTML=data[0].result_data;
							}else if(data[0].case_del == "db_del"){
								alert(data[0].comment);
								document.getElementById("img_db").innerHTML=data[0].result_data;
							}
						}
					}
				};
			req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
			req.send(value_url);
}

function fn_delete_img(){
	var req=Inint_AJAX();
	var url=null;
	var value_url=null;

	if(document.getElementById("photo_history")){
		if(document.getElementById("photo_history").value != ""){
			url='unlink_image.php';
			value_url='path_image='+encodeURIComponent(document.getElementById('photo_history').value);
			value_url+='&hash='+new Date().getTime()+Math.random();

			req.open("POST", url, true);
				req.onreadystatechange = function() {
					if (req.readyState==4) {
						if (req.status==200) {
							var data=eval(req.responseText);
						}
					}
				};
			req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
			req.send(value_url);
		}
	}
}

function fn_delete(url_element, id_element){
	var req=Inint_AJAX();
	var url=null;
	var value_url=null;
	
	if(confirm('Confirm Delete ?')==false){
		return 0;
	}
	url='delete_'+url_element+'.php';

	value_url='id='+id_element;
	value_url+='&hash='+new Date().getTime()+Math.random();
	
	req.open("POST", url, true);
			req.onreadystatechange = function() {
					if (req.readyState==4) {
						if (req.status==200) {
							var data=eval(req.responseText);
							if(data[0].success == "false"){
								return alert(data[0].comment);
							}
							alert(data[0].comment);
							fn_cancel(url_element);
						}
					}
			};
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;"); // setHeader charset=tis-620
	req.send(value_url);

	//return alert('Delete ID='+id_element);
}

function chk_payment(id_element){
	var url='check_payment.php?id='+id_element+'&hash='+new Date().getTime()+Math.random();
	window.open(url, '_blank','scrollbars=yes,resizable=no,width=550,height=320');
}

function account_submit(id_element){
	var req=Inint_AJAX();
	var url=null;
	var value_url=null;
	
	if(confirm('Confirm the transfer. ?')==false){
		return 0;
	}

	url='account_confirm.php';

	value_url='id='+id_element;
	value_url+='&hash='+new Date().getTime()+Math.random();
	
	req.open("POST", url, true);
			req.onreadystatechange = function() {
					if (req.readyState==4) {
						if (req.status==200) {
							var data=eval(req.responseText);
							if(data[0].success == "false"){
								return alert(data[0].comment);
							}
							alert(data[0].comment);
							goto_page('st_ord_purchase');
						}
					}
			};
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;"); // setHeader charset=tis-620
	req.send(value_url);
}

function logistic_submit(id_element){
	var req=Inint_AJAX();
	var url=null;
	var value_url=null;
	//var track_id=prompt("กรุณากรอก Track Order number ?", "");
	menudelayHide('submenu1');
	var track_id=document.getElementById("track_id").value;
	var obj_hh=document.getElementById("hh");
	var hh=obj_hh.options[obj_hh.selectedIndex].value;
	var obj_mm=document.getElementById("mm");
	var mm=obj_mm.options[obj_mm.selectedIndex].value;

	var deliver_date=document.getElementById("deliver_date").value+' '+hh+':'+mm;
	//alert(deliver_date);
	if(track_id == null){
		alert('คุณไม่ได้กรอกข้อมูล !!!');
		return 0;
	}

	alert('Track ID : '+track_id+'\nDeliver Date : '+deliver_date);

	if(confirm('Confirm Sure ?')==false){
		alert('คุณได้ยกเลิกการยืนยันเรื่องการจัดส่ง');
		return 0;
	}

	url='logistic_confirm.php';

	value_url='id='+id_element;
	value_url+='&track_id='+encodeURIComponent(track_id);
	value_url+='&deliver_date='+encodeURIComponent(deliver_date);
	value_url+='&hash='+new Date().getTime()+Math.random();
	
	req.open("POST", url, true);
			req.onreadystatechange = function() {
					if (req.readyState==4) {
						if (req.status==200) {
							var data=eval(req.responseText);
							alert(data[0].comment);
							if(data[0].success == "false"){
								return 0;
							}
							goto_page('st_ord_purchase');
						}
					}
			};
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;"); // setHeader charset=tis-620
	req.send(value_url);
}

function ch_comment_prd(id_element, status){
	var req=Inint_AJAX();
	var url=null;
	var value_url=null;

	if(confirm('Confirm Sure ?')==false){
		alert('คุณได้ทำการยกเลิกการ');
		return 0;
	}

	url='ch_comment_prd.php';

	value_url='id='+id_element;
	value_url+='&status='+status;
	value_url+='&hash='+new Date().getTime()+Math.random();
	
	req.open("POST", url, true);
			req.onreadystatechange = function() {
					if (req.readyState==4) {
						if (req.status==200) {
							var data=eval(req.responseText);
							alert(data[0].comment);
							if(data[0].success == "false"){
								return 0;
							}
							goto_page('comment_prd');
						}
					}
			};
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	req.send(value_url);
}

function permision_user(id_element, status){
	var req=Inint_AJAX();
	var url=null;
	var value_url=null;

	if(confirm('Confirm Sure ?')==false){
		alert('คุณได้ทำการยกเลิกการ Lock Customer');
		return 0;
	}

	url='permision_user.php';

	value_url='id='+id_element;
	value_url+='&status='+status;
	value_url+='&hash='+new Date().getTime()+Math.random();
	
	req.open("POST", url, true);
			req.onreadystatechange = function() {
					if (req.readyState==4) {
						if (req.status==200) {
							var data=eval(req.responseText);
							alert(data[0].comment);
							if(data[0].success == "false"){
								return 0;
							}
							goto_page('customer');
						}
					}
			};
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	req.send(value_url);
}

function goto_report(url_element){
	var req=Inint_AJAX();
	var url=null;
	var value_url=null;
	
	url='rpt'+url_element+'.php';
	value_url='mode=link';

	if(url_element == "member"){
		document.getElementById("title_report").innerHTML='รายงานลูกค้าสมาชิก';
	}else if(url_element == "stock_product"){
		document.getElementById("title_report").innerHTML='รายงานสต็อกสินค้า';
	}else if(url_element == "check_buy"){
		document.getElementById("title_report").innerHTML='รายงานการสั่งซื้อสินค้าเข้าจาำกตัวแทนจำหน่าย';
	}else if(url_element == "sales"){
		document.getElementById("title_report").innerHTML='รายงานยอดขายสินค้า';
	}

	value_url+='&hash='+new Date().getTime()+Math.random();

	req.open("POST", url, true);
			req.onreadystatechange = function() {
					if (req.readyState==4) {
						if (req.status==200) {
							var data=eval(req.responseText);
							document.getElementById("rp_data").innerHTML=data[0].result_data;
						}
					}
			};
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	req.send(value_url);
}

function page_rp(page_num, element){
	var req=Inint_AJAX();
	var url=null;
	var value_url=null;
	var cm1, cm2, yid3, yid4;
	var len_chk;
	var check_type, rdo;
	var form_chk;
	
	url='rpt'+element+'.php';
	value_url='page='+page_num;
		if(document.getElementById("chk_mode")){
			value_url+='&mode='+encodeURIComponent(document.getElementById("chk_mode").value);
		}

		if(document.getElementById("keyword")){
			var keyword=encodeURIComponent(document.getElementById("keyword").value);
			value_url+='&keyword='+keyword;
		}
		
		if(document.getElementById("str_bdate")){
			value_url+='&str_bdate='+encodeURIComponent(document.getElementById("str_bdate").value);
		}
		if(document.getElementById("str_edate")){
			value_url+='&str_edate='+encodeURIComponent(document.getElementById("str_edate").value);
		}
		if(document.getElementById("check_type")){
			form_chk=document.form_check_buy;
			len_chk=form_chk.elements["check_type"].length;
			for(i=0;i<len_chk;i++){
				rdo=form_chk.elements["check_type"][i];
				if(rdo.checked){
					check_type=rdo.value;
					break;
				}
			}
			if(check_type == 1){
				cm1=document.getElementById("yid1").value+'-'+document.getElementById("mid1").value;
				cm2=document.getElementById("yid2").value+'-'+document.getElementById("mid2").value;
				value_url+='&cm1='+encodeURIComponent(cm1)+'&cm2='+encodeURIComponent(cm2);
			}else{
				yid3=document.getElementById("yid3").value;
				yid4=document.getElementById("yid4").value;
				value_url+='&yid3='+encodeURIComponent(yid3)+'&yid4='+encodeURIComponent(yid4);
			}
			value_url+='&check_type='+check_type;
		}

	value_url+='&hash='+new Date().getTime()+Math.random();

	req.open("POST", url, true);
			req.onreadystatechange = function() {
					if (req.readyState==4) {
						if (req.status==200) {
							var data=eval(req.responseText);
							document.getElementById("rp_"+element).innerHTML=data[0].result_data;
						}
					}
			};
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	req.send(value_url);
}

function search_rp(type, element){
	var req=Inint_AJAX();
	var url=null;
	var value_url=null;
	var cm1, cm2, yid3, yid4;
	var len_chk;
	var check_type, rdo;
	var form_chk;
	
	url='rpt'+element+'.php';
	value_url='mode=search';
	if(type == "null"){
		if(document.getElementById("keyword")){
			var keyword=encodeURIComponent(document.getElementById("keyword").value);
			value_url+='&keyword='+keyword;
		}
		if(document.getElementById("str_bdate")){
			value_url+='&str_bdate='+encodeURIComponent(document.getElementById("str_bdate").value);
		}
		if(document.getElementById("str_edate")){
			value_url+='&str_edate='+encodeURIComponent(document.getElementById("str_edate").value);
		}
		if(document.getElementById("check_type")){
			form_chk=document.form_check_buy;
			len_chk=form_chk.elements["check_type"].length;
			for(i=0;i<len_chk;i++){
				rdo=form_chk.elements["check_type"][i];
				if(rdo.checked){
					check_type=rdo.value;
					break;
				}
			}
			if(check_type == 1){
				cm1=document.getElementById("yid1").value+'-'+document.getElementById("mid1").value;
				cm2=document.getElementById("yid2").value+'-'+document.getElementById("mid2").value;
				value_url+='&cm1='+encodeURIComponent(cm1)+'&cm2='+encodeURIComponent(cm2);
			}else{
				yid3=document.getElementById("yid3").value;
				yid4=document.getElementById("yid4").value;
				value_url+='&yid3='+encodeURIComponent(yid3)+'&yid4='+encodeURIComponent(yid4);
			}
			value_url+='&check_type='+check_type;
		}
	}else{
		if(document.getElementById("keyword")){
			document.getElementById("keyword").value ='';
		}
	}

	value_url+='&hash='+new Date().getTime()+Math.random();

	req.open("POST", url, true);
			req.onreadystatechange = function() {
					if (req.readyState==4) {
						if (req.status==200) {
							var data=eval(req.responseText);
							document.getElementById("rp_"+element).innerHTML=data[0].result_data;
						}
					}
			};
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	req.send(value_url);
}

//-------------------------------- OnMouseOver To Menu ------------------------------------ 
function getposOffset(overlay, offsettype){
  var totaloffset = (offsettype == "left")? overlay.offsetLeft : overlay.offsetTop
  var parentEl = overlay.offsetParent
  while (parentEl != null) {
    totaloffset = (offsettype == "left")? totaloffset + parentEl.offsetLeft : totaloffset + parentEl.offsetTop
    parentEl = parentEl.offsetParent
  }
  return totaloffset
}

function menuoverlay(curobj, subobjstr) {
	var id_element=subobjstr;
	curobj.onclick=function(){ menudelayHide(curobj, subobjstr); }
	var chk_browser=document.getElementById("chk_browser").value;
	var x_style, y_style;
  if (document.getElementById) {
    if (menudisplay != "" & menudisplay != subobjstr) document.getElementById(menudisplay).style.visibility = "hidden";
    menudisplay = subobjstr;
    var subobj = document.getElementById(subobjstr);
    subobj.style.visibility = "visible";
    var xpos = getposOffset(curobj, "left")
    var ypos = getposOffset(curobj, "top")

     /* คำนวณตำแหน่งของเมนูตรงกับด้านล่าง-ซ้าย */
	x_style=0;
	y_style=0;
	if(chk_browser == "MSIE"){
		if(id_element == "submenu1"){
			x_style=240;
			y_style=88;
		}
	}
	  /*}else if(chk_browser == "U"){
		x_style=100;
		y_style=10;
	 }*/
	
    subobj.style.left = (xpos-x_style) + "px"
    subobj.style.top = (ypos-y_style) + curobj.offsetHeight+2+ "px"
    
    return false
  }
}

/* หน่วงเวลาเพื่อซ่อนเมนู */
/*function menudelayHide(lyr) {
  document.getElementById("logistic_link").onclick=function(){ menuoverlay(this, 'submenu1'); }
  menutimedelay = setTimeout('document.getElementById("'+lyr+'").style.visibility="hidden"', 250);
}*/

function menudelayHide(obj_clk, lyr) {
	if(obj_clk){
		obj_clk.onclick=function(){ menuoverlay(this, lyr); }
	}
  menutimedelay = setTimeout('document.getElementById("'+lyr+'").style.visibility="hidden"', 250);
}

/* หยุดการซ่อนเมนู */
function menuMouseover(lyr) {
  if (menutimedelay > 0) clearTimeout(menutimedelay)
}

/* ซ่อนเมนูทันที */
function menuHide(lyr) {
  document.getElementById(lyr).style.visibility = "hidden"
}